package ststat.project2;

import java.math.BigInteger;
import java.util.ArrayList;

public class Calculator {

    public static double nCr(double n, double r) {

        return (fact(n).divide((fact(r).multiply(fact(n - r))))).doubleValue();
    }

    public static BigInteger fact(double n) {

        BigInteger x = BigInteger.valueOf(1);

        for (int i = 1; i <= n; i++) {
            x = x.multiply(BigInteger.valueOf(i));
        }
        return x;
    }

    public static ArrayList<DataPoint> hypergeometricDistribution(int lowerX, int upperX, int N, int n, int k) {
        ArrayList<DataPoint> x = new ArrayList<DataPoint>();
        
        for (int i = lowerX; i <= upperX; i++) {
            DataPoint item = new DataPoint(i, hypergeometric(i,N,n,k));
            x.add(item);
        }

        return x;
    }
    
    public static ArrayList<DataPoint> hypergeometricDistributionN(int x, int N, int lowerN, int upperN, int k) {
        ArrayList<DataPoint> distribution = new ArrayList<DataPoint>();
        
        for (int i = lowerN; i <= upperN; i++) {
            DataPoint item = new DataPoint(i, hypergeometric(x,N,i,k));
            distribution.add(item);
        }

        return distribution;
    }
    
    public static ArrayList<DataPoint> hypergeometricDistributionK(int x, int N, int n, int lowerK, int upperK) {
        ArrayList<DataPoint> distribution = new ArrayList<DataPoint>();
        
        for (int i = lowerK; i <= upperK; i++) {
            DataPoint item = new DataPoint(i, hypergeometric(x,N,n,i));
            distribution.add(item);
        }

        return distribution;
    }

    public static double hypergeometric(int x, int N, int n, int k) {
        return (nCr(k, x) * nCr(N - k, n - x)) / nCr(N, n);
    }

    public static double total(ArrayList<DataPoint> table) {
        double x = 0;
        for (int i = 0; i < table.size(); i++) {
            x = x + table.get(i).getValue();
        }
        return x;
    }
}
